package com.example.wangzh.mytest.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.wangzh.mytest.R;


public class TopTitleBar extends RelativeLayout {

    private Context mContext;
    private ImageView iv_title_left;
    private TextView tv_title_center;
    private RelativeLayout rll_title_right, rll_img_or_ckb;
    private TextView tv_title_right;
    private ImageView img_title_right;
    private CheckBox ckb_title_right;
    private String titleText;


    public TopTitleBar(Context context) {
        this(context, null);
    }

    public TopTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.custom_top_title_bar, this);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title_center = (TextView) findViewById(R.id.tv_title_center);
        rll_title_right = (RelativeLayout) findViewById(R.id.rll_title_right);
        tv_title_right = (TextView) findViewById(R.id.tv_title_right);
        img_title_right = (ImageView) findViewById(R.id.img_title_right);
        ckb_title_right = (CheckBox) findViewById(R.id.ckb_title_right);
        rll_img_or_ckb = (RelativeLayout) findViewById(R.id.rll_img_or_ckb);
        if (isInEditMode()) {
            return;
        }
        loadAttrs(attrs, defStyleAttr);
        if (titleText != null) {
            tv_title_center.setText(titleText);
        }
        iv_title_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof Activity) {
                    ((Activity) mContext).finish();
                }
            }
        });
    }

    private void loadAttrs(AttributeSet attrs, int defStyleAttr) {
        if (attrs == null) {
            return;
        }

//       这里getTheme()也有可能是style，在Toptitlebar里面设置style="@style/topTitle"也能检测到
//		设置android:theme="@style/topTitle"反而会出问题导致抛出，后面的iv_title_left.setOnClickListener没有执行
        TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs,
                R.styleable.TopTitleBar, defStyleAttr, 0);
        //将这里的defStyleRes直接设为当前theme可以消除乱码，但这样写不合适
        int n = a.getIndexCount();
        for (int i = 0; i <= n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
//                自定义控件直接从当前theme获取字符串时会出现乱码不知道为什么
                case R.styleable.TopTitleBar_TopTitleBar_title:
                    titleText = a.getString(attr);
                    break;
                case R.styleable.TopTitleBar_TopTitleBar_color:
                    tv_title_center.setTextColor(a.getColor(attr, 0));
                    break;
                default:
                    break;
            }
        }
    }

    public void setStateOnlyTitle() {
        iv_title_left.setVisibility(View.INVISIBLE);
        tv_title_center.setVisibility(View.VISIBLE);
        rll_title_right.setVisibility(View.VISIBLE);
    }

    public void setRightText(String str) {
        tv_title_right.setVisibility(View.VISIBLE);
        tv_title_right.setText(str);
    }

    public void setLeftImage(int resId) {
        iv_title_left.setImageResource(resId);
    }

    public void setRightImage(int resId) {
        img_title_right.setVisibility(View.VISIBLE);
        ckb_title_right.setVisibility(View.GONE);
        img_title_right.setImageResource(resId);
        rll_img_or_ckb.setVisibility(VISIBLE);
    }

    public void setRightCkbResource(int resId) {
        img_title_right.setVisibility(View.GONE);
        ckb_title_right.setVisibility(View.VISIBLE);
        ckb_title_right.setButtonDrawable(resId);
        rll_img_or_ckb.setVisibility(VISIBLE);
//		rll_title_right.setOnClickListener(new OnClickListener( ) {
//			@Override
//			public void onClick(View v) {
//				ckb_title_right.setChecked(!ckb_title_right.isChecked( ));
//				if (ckb_title_right.isChecked( )) {
//					tv_title_right.setTextColor(getResources( ).getColor(R.color.yellow));
//				} else {
//					tv_title_right.setTextColor(getResources( ).getColor(R.color.text_color__black));
//				}
//			}
//		});
    }

    public void changeRightCkbState() {
        ckb_title_right.setChecked(!ckb_title_right.isChecked());
        if (ckb_title_right.isChecked()) {
            tv_title_right.setTextColor(getResources().getColor(R.color.yellow));
        } else {
            tv_title_right.setTextColor(getResources().getColor(R.color.text_color__black));
        }
    }

    public void setRightCkbState(boolean check) {
        ckb_title_right.setChecked(check);
        if (ckb_title_right.isChecked()) {
            tv_title_right.setTextColor(getResources().getColor(R.color.yellow));
        } else {
            tv_title_right.setTextColor(getResources().getColor(R.color.text_color__black));
        }
    }


    /**
     * @param title
     * @Description: ���ñ�������
     */
    public void setTitleText(String title) {
        this.titleText = title;
        tv_title_center.setText(title);
    }

    /**
     * @param listener
     * @Description: ��߰�ť����¼�
     */
    public void setBtnLeftOnclickListener(OnClickListener listener) {
        iv_title_left.setOnClickListener(listener);
    }

    public void setRightOnclickListener(OnClickListener listener) {
        rll_title_right.setOnClickListener(listener);
    }

    public void setRightVisibility(int visible) {
        rll_title_right.setVisibility(visible);
    }

    public void setRightTextColor(int color) {
        tv_title_right.setTextColor(color);
    }

}
